<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\api\logic;
use app\api\BaseModel;
use think\Db;

class Order extends BaseModel{
    //检测参数是否已传
    protected $ParameterDetection  = [
        'orderAdd'=>['address_id','goods','preference','cart_id'],
        'orderPreview'=>['type'],
        'orderInfo'=>['order_id','type'],
        'orderUpdate'=>['order_id','order_status','goods_id','order_goods_status'],
        'orderReturn'=>['order_id','goods_id','remarks'],
        'orderReturnUpdate'=>['order_id','goods_id','status'],
        'orderReturnImg'=>['return_id'],
        'orderReturnInfo'=>['return_id'],
        'orderReview'=>['order_id','goods_id','content']
    ];


	/**
	* 订单列表
	*/
	public function orderList(){
        $type = isset($this->requestData['type'])?$this->requestData['type']:'1';
        $page = isset($this->requestData['page'])?$this->requestData['page']:'1';
        $status = isset($this->requestData['status'])?$this->requestData['status']:'';
        $data = [];
        $where = [];

        if($status != ''){
            if(!in_array($status, ['0','1','3','-2']))return ['message'=>'status参数有误'];
            $where['or.status'] = $status;
        }else{
            $where['or.status'] = ['neq','-2'];
        }

        if(!in_array($type, ['1','2']))return ['message'=>'type参数有误'];

        if($type == '1'){
            //自己下的单
            $where['or.user_id'] = $this->user['id'];
        }

        if($type == '2'){
            //待购订单
            $where['og.buyer_user_id'] = $this->user['id'];
            $where['or.status'] = '1';
        }


        $order_result = db('order')->alias('or')->join('order_goods og','og.order_id = or.id')->where($where)->page($page,'10')->order('or.id desc')->field('og.goods_name,og.goods_id,og.order_id,og.goods_price,og.goods_image,or.total_count,or.total_price,or.status as order_status,og.status as order_good_status,og.goods_num as order_goods_num,or.update_time')->select();

        $order_ids = array_column($order_result, 'order_id');
        $order_ids = array_values(array_unique($order_ids));

        foreach ($order_ids as $k => $v) {
            foreach ($order_result as $kk => $vv) {
                if($vv['order_id'] == $v){

                    if(!isset($data[$k]['order_total'])){
                        $data[$k]['order_total'] = $vv['total_count'];
                    }

                    if(!isset($data[$k]['order_total_price'])){
                        $data[$k]['order_total_price'] = $vv['total_price']/100;
                    }

                    if(!isset($data[$k]['order_status'])){
                        $data[$k]['order_status'] = $vv['order_status'];
                    }

                    unset($vv['total_count']);
                    unset($vv['total_price']);
                    unset($vv['order_status']);
                    $vv['goods_price'] = $vv['goods_price']/100;
                    $vv['goods_image'] = IMG_PATH.$vv['goods_image'];

                    if($type == '2' && $vv['order_good_status'] != '5'){
                        if($vv['order_good_status'] == '2'){
                            //买家订单时间操作
                            $order_time = config('order_processing.time');
                            $order_new_time = time()-$vv['update_time'];
                            if($order_time < $order_new_time){
                                //是否现实已完成
                                $vv['is_update_completed'] = '1';
                            }else{
                                $vv['is_update_completed'] = '0';
                            }
                        }else{
                            $vv['is_update_completed'] = '0';
                        }
                    }else{
                        $vv['is_update_completed'] = '0';
                    }

                    $data[$k]['order_goods'][] = $vv;
                }
            }
            $pay_sn = db('pay_log')->where(['record_id'=>$v,'status'=>['in',['0','-1']]])->field('pay_sn')->find();
            $data[$k]['order_id'] = $v;
            $data[$k]['pay_sn'] = $pay_sn['pay_sn'];
        }

        return $data;
	}

    /**
     * 订单详情
     */
    public function orderInfo(){
        $order_id = $this->requestData['order_id'];
        $type = isset($this->requestData['type'])?$this->requestData['type']:'1';
        $result = [];

        if(!in_array($type, ['1','2']))return ['message'=>'type参数有误'];

        $order_result = db('order')->where(['id'=>$order_id])->field('')->find();
        if(empty($order_result))return ['message'=>'order_id参数有误!'];
        $result['order_total_price'] = $order_result['total_price']/100;
        $result['order_total_count'] = $order_result['total_count'];
        $result['order_address'] = $order_result['address'];
        $result['order_mobile'] = $order_result['mobile'];
        $result['order_preference'] = $order_result['preference'];
        $result['order_id'] = $order_id;

        $where['order_id'] = $order_id;

        if($type == '2'){
            //待购订单
            $where['buyer_user_id'] = $this->user['id'];
        }


        $order_goods = db('order_goods')->where($where)->order('id desc')->select();
		
        $circle_name = array_column($order_goods, 'circle_name');
        $circle_name = array_values(array_unique($circle_name));

        foreach ($circle_name as $k => $v) {
            $result['order_goods'][$k]['circle_name'] = $v;
            foreach ($order_goods as $kk => &$vv) {
                if($vv['circle_name'] == $v){
                    $result['order_goods'][$k]['circle_name'] = $v;
                    $vv['goods_price'] = $vv['goods_price']/100;
                    $vv['goods_image'] = IMG_PATH.$vv['goods_image'];
                    $result['order_goods'][$k]['goods_result'][] = $vv;
                }
            }
        }

        return $result;      
    }

    /**
    * 预览订单
    */
    public function orderPreview(){
        $type = $this->requestData['type'];
        $data = [];

        if(!in_array($type,['1','2']))return ['message'=>'参数有误!'];

        $user_address = db('user_address')->where(['user_id'=>$this->user['id']])->select();

        if(empty($user_address))return ['message'=>'你还没填写收货地址'];
        
        $result = [];
        if($type == '1'){
            //直接购买
            $goods_id = isset($this->requestData['goods_id'])?$this->requestData['goods_id']:'';
            $goods_id = intval($goods_id);
            $circle_goods_result = db('circle_goods')->where(['id'=>$goods_id])->find();
            if(empty($goods_id))return ['message'=>'请选择商品!'];
            if($circle_goods_result['stock'] == 0)return ['message'=>'该商品已没库存!'];

            $circle_result = db('circle')->where(['id'=>$circle_goods_result['cid']])->find();
            if($circle_result['is_del'] == '-1')return ['message'=>'该买手圈已删除!'];
            if($circle_result['type'] == '2')return ['mesasge'=>'请到买手圈下单!'];

            $result[0]['circle_name'] = $circle_result['circle_name'];
            $result[0]['goods_result'][0]['goods_price'] = $circle_goods_result['price']/100;
            $result[0]['goods_result'][0]['cid'] = $circle_goods_result['cid'];
            $result[0]['goods_result'][0]['goodsid'] = $goods_id;
            $result[0]['goods_result'][0]['goods_name'] = $circle_goods_result['name'];
            $result[0]['goods_result'][0]['goods_stock'] = $circle_goods_result['stock'];
            if(isset($circle_goods_result['goods_image'])){
                $circle_goods_result['goods_image'] = IMG_PATH.$circle_goods_result['goods_image'];
            }else{
                $circle_goods_result['goods_image'] = '';
            }
            $result[0]['goods_result'][0]['goods_image'] = $circle_goods_result['goods_image'];
            $result[0]['goods_result'][0]['goods_total'] = 1;
        }else{
            //购物车结算
            $cart_id = isset($this->requestData['cart_id'])?$this->requestData['cart_id']:'';
            $cart_id = explode(',', $cart_id);

            $cart_result = db('cart')->where(['id'=>['in',$cart_id],'user_id'=>$this->user['id']])->order('cid desc')->select();
            
            if(!empty($cart_result)){
                $circle_name = array_column($cart_result, 'circle_name');
                $circle_name = array_unique($circle_name);

                $cids = array_column($cart_result,'cid');

                $circle_result = db('circle')->where(['id'=>['in',$cids]])->field('circle_name,is_del')->select();
                
                //删除了就不允许下单
                $circle_bool = ['bool'=>'true','no_message'=>''];
                foreach ($circle_result as $key => $value) {
                    if($value['is_del'] == '-1'){
                        $circle_bool['bool'] = 'false';
                        $circle_bool['no_message'] = $value['circle_name'];
                        break;
                    }
                }

                if($circle_bool['bool'] == 'false')return ['message'=>'\''.$circle_bool['no_message'].'\'需求已删除，关于该商品已不能购买!'];

                foreach ($circle_name as $k => $v) {
                    $result[$k]['circle_name'] = $v;
                    foreach ($cart_result as $kk => &$vv) {
                        if($vv['circle_name'] == $v){
                            //查询库存
                            $circle_goods_stock = db('circle_goods')->where(['id'=>$vv['goodsid']])->field('stock')->find();
                            $vv['goods_price'] = $vv['goods_price']/100;
                            if(isset($vv['goods_image'])){
                                $vv['goods_image'] = IMG_PATH.$vv['goods_image'];
                            }else{
                                $vv['goods_image'] = '';
                            }
                            $vv['goods_total'] = $vv['total'];
                            $vv['goods_stock'] = $circle_goods_stock['stock'];

                            //删除相关参数
                            unset($vv['user_id']);
                            unset($vv['total']);
                            $result[$k]['goods_result'][] = $vv;
                        }
                    }
                }
            }else{
                return ['message'=>'购物车为空!'];
            }
        }

        //计算有多少个商品,计算总价
        $goods_total_num = 0;
        $goods_total_price = 0;

        $stock_bool = $this->stock_bool($result);

        if($stock_bool['message'] == 'false'){
            return ['message'=>'\''.$stock_bool['no_goods']['goods_name'].'\'库存不足!'];
        } 

        foreach ($result as $k => $v) {
            foreach ($v['goods_result'] as $kk => $vv) {
                $goods_total_num += $vv['goods_total'];
                $goods_total_price += $vv['goods_price']*$vv['goods_total'];
            }
        }

        $result = array_merge($result);
        $data['goods_result'] = $result;
        $data['goods_total_num'] = $goods_total_num;
        $data['goods_total_price'] = $goods_total_price;
        $data['user_address'] = $user_address;

        return $data;
    }

    /**
    * 生成订单
    */
    public function orderAdd(){
        $address_id = $this->requestData['address_id'];     //收货地址
        $goods = $this->requestData['goods'];               //购买商品
        $preference = $this->requestData['preference'];     //备注
        $cart_id = $this->requestData['cart_id'];           //删除购物车
        $cart_id = explode(',', $cart_id);

        $order_data = [];
        $order_goods_data = [];
        
        if(empty($goods))return ['message'=>'goods产品没传!'];

        $goods = @json_decode($goods,true);
        $error = json_last_error();
        if(!empty($error))return ['message'=>'goods参数有误!'];
        if(!is_array($goods))return ['message'=>'goods参数有误!'];
        array_multisort(array_column($goods, 'goodsid'),SORT_DESC,$goods);

        $goodsid = array_column($goods, 'goodsid');
        $goodsid_count = count($goodsid);
        $circle_goods = db('circle_goods')->where(['id'=>['in',$goodsid],'status'=>'1'])->order('id desc')->select();
        $circle_goods_count = count($circle_goods);
        if(($circle_goods_count != $goodsid_count) || empty($circle_goods))return ['message'=>'产品不存在或已下架!'];

        $user_address = db('user_address')->where(['address_id'=>$address_id])->find();
        if(empty($user_address))return ['message'=>'请填写收货地址!'];

        $cids = array_column($goods,'cid');
        
        $circle_result = db('circle')->where(['id'=>['in',$cids]])->field('circle_name,is_del')->select();
                
        //删除了就不允许下单
        $circle_bool = ['bool'=>'true','no_message'=>''];
        foreach ($circle_result as $key => $value) {
            if($value['is_del'] == '-1'){
                $circle_bool['bool'] = 'false';
                $circle_bool['no_message'] = $value['circle_name'];
                break;
            }
        }

        if($circle_bool['bool'] == 'false')return ['message'=>'\''.$circle_bool['no_message'].'\'需求已删除，关于该商品已不能购买!'];
        
        $total_price = 0;   //订单总价
        $total = 0;         //商品总数量

        $stock_bool = ['message'=>'true','no_goods'=>[]];   //库存是否充足
        $goods_param = ['message'=>'true','no_param'=>''];  //检验参数问题
         
        //订单商品
        foreach($goods as $k=>$v){
            $stock = $circle_goods[$k]['stock'];            //用户
            if(empty($v['circle_name']) || empty($v['cid']) || empty($v['total'])){
                $goods_param['message'] = 'false';
                $goods_param['no_param'] = 'goods参数有误';
                break;
            }

            $circle = db('circle')->where(['id'=>$v['cid']])->field('user_id')->find();
            $buyer_user_id = $circle['user_id'];            //发布者
            
            if(empty($buyer_user_id)){
                $goods_param['message'] = 'false';
                $goods_param['no_param'] = 'goods参数有误';
                break;
            }

            if($stock < $v['total']){
                $stock_bool['message'] = 'false';
                $stock_bool['no_goods']['goods_name'] = $circle_goods[$k]['name'];
                break;
            }

            $order_goods_data[$k]['circle_name'] = $v['circle_name'];
            $order_goods_data[$k]['buyer_user_id'] = $buyer_user_id;
            $order_goods_data[$k]['cid'] = $v['cid'];
            $order_goods_data[$k]['goods_id'] = $v['goodsid'];
            $order_goods_data[$k]['goods_name'] = $circle_goods[$k]['name'];
            $order_goods_data[$k]['goods_num'] = $v['total'];
            $order_goods_data[$k]['goods_price'] = $circle_goods[$k]['price'];
            $order_goods_data[$k]['goods_image'] = $circle_goods[$k]['image'];

            $total_price += ($circle_goods[$k]['price']) * $v['total'];
            $total += $v['total'];
        }
        
        if($stock_bool['message'] == 'false')return ['message'=>'\''.$stock_bool['no_goods']['goods_name'].'\'库存不足!'];
        if($goods_param['message'] == 'false')return ['message'=>$goods_param['no_param']];

        //订单
        $order_data['order_sn'] = $this->generateOrderNo();
        $order_data['total_price'] = $total_price;
        $order_data['total_count'] = $total;
        $order_data['product_img'] = $circle_goods[0]['image'];
        $order_data['product_name'] = $circle_goods[0]['name'];
        $order_data['user_id'] = $this->user['id'];


        $address = $user_address['province'].$user_address['city'].$user_address['country'].$user_address['street'].$user_address['detail'];
        $order_data['address'] = $address;
        $order_data['preference'] = $preference;
        $order_data['create_time'] = time();
        $order_data['mobile'] = $user_address['mobile'];
        
        // 启动事务
        Db::startTrans();
        try{
            $order_id = db('order')->insertGetId($order_data);
            foreach ($order_goods_data as $key => &$value) {
                $value['order_id'] = $order_id;
            }
            db('order_goods')->insertAll($order_goods_data);

            //减库存
            foreach ($goods as $key => $value) {
                db('circle_goods')->where(['id'=>$value['goodsid']])->setDec('stock',$value['total']);
            }

            //删除购物车
            if(is_array($cart_id) && !empty($cart_id)){
                db('cart')->where(['id'=>['in',$cart_id]])->delete();
            }

            $pay_info = array(
                'pay_sn' => 'P'.$this->generateOrderNo(),
                'money' => $order_data['total_price'],
                'memid' => $this->user['id'],
                'create_time' => $order_data['create_time'],
                'mem_name' => $this->user['nickname'],
                'record_id' => $order_id
            );

            db('pay_log')->insert($pay_info);

            // 提交事务
            Db::commit();    
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return array('message' => $e->getMessage());
        }
        
        if($order_id){
            $result = [];
            $result['order_sn'] = $order_data['order_sn'];
            $result['pay_sn'] = $pay_info['pay_sn'];
            $result['order_id'] = $order_id;
            $result['create_time'] = $order_data['create_time'];

            return $result;
        }else{
            return ['message'=>'生成订单失败!'];
        }
    }

    /**
    * 订单状态更新
    */
    public function orderUpdate(){
        $order_status = $this->requestData['order_status'];
        $order_goods_status = $this->requestData['order_goods_status'];
        $order_id = $this->requestData['order_id'];
        $goods_id = $this->requestData['goods_id'];

        //order_goods status '0待付款，1已付款，2，处理中，3确认收货，4申请退货，5已完成'
        //order status '0待付款，1已付款，3已完成，-2已关闭'
        if(!empty($order_status)){
            if(!in_array($order_status, ['3','-2']))return ['message'=>'order_status参数有误'];
        }

        if(!empty($order_goods_status)){
            if(!in_array($order_goods_status, ['3','2','5']))return ['message'=>'order_goods_status参数有误'];
        }

        $order_result = db('order')->where(['id'=>$order_id])->find();

        if(empty($order_result))return ['message'=>'order_id参数有误!'];

        if(empty($goods_id)){
            if($order_result['user_id'] != $this->user['id'])return ['message'=>'你没有权限更改别人的订单!'];
            if(empty($order_status))return ['message'=>'order_status不能为空!'];
            if($order_result['status'] == $order_status)return ['message'=>'已操作过了'];

            if($order_status == '-2' && $order_result['status'] != '0'){
                if($order_result['status'] == '1')return ['message'=>'请先把商品订单完成以后，在关闭订单!'];
            }

            if($order_status == '3'){
                if($order_result['status'] != '1')return ['message'=>'请先支付订单!'];
                $order_goods_status_count = db('order_goods')->where(['status'=>'5','order_id'=>$order_id])->count();
                $order_goods_count = db('order_goods')->where(['order_id'=>$order_id])->count();
                if($order_goods_status_count != $order_goods_count)return ['message'=>'请先把商品订单完成以后,在操作该订单状态!'];
            }

            $order_handle = db('order')->where(['id'=>$order_id,'user_id'=>$this->user['id']])->update(['status'=>$order_status]);
        }else{
            $order_goods_result = db('order_goods')->where(['order_id'=>$order_id,'goods_id'=>$goods_id])->find();
            if(empty($order_goods_status))return ['message'=>'order_goods_status不能为空!'];
            if(empty($order_goods_result))return ['message'=>'该订单没有这商品'];

            if($order_goods_result['status'] == $order_goods_status)return ['message'=>'已操作过了'];

            if($order_goods_status == '2'){
                if($order_goods_result['status'] != '1')return ['message'=>'请先付款在处理订单!'];
                if($order_goods_result['buyer_user_id'] != $this->user['id'])return ['message'=>'你没有权利更改别人的订单!'];
            }
                
            if($order_goods_status == '3'){
                if($order_goods_result['status'] != '2')return ['message'=>'请等待买家处理在确认收货!'];
            }

            if($order_goods_status == '5'){

                if(!in_array($this->user['id'], [$order_result['user_id'],$order_goods_result['buyer_user_id']]))return ['message'=>'你没有权利更改别人的订单!'];
                $pay_log = db('pay_log')->where(['record_id'=>$order_id])->value('status');

                if($pay_log != '1')return ['message'=>'该订单未付款!'];

                $order_return = db('order_return')->where(['order_id'=>$order_id,'goods_id'=>$goods_id])->order('id desc')->find();

                if($order_goods_result['status'] == '4'){
                    if($order_return['status'] == '0')return ['message'=>'退货流程未审核!'];
                    if($order_return['money_arrives_status'] == '0')return ['message'=>'退货金额未到账!'];
                }

                //买家订单时间操作
                $order_time = config('order_processing.time');

                if($this->user['id'] == $order_goods_result['buyer_user_id']){
                    if($order_goods_result['status'] != '2')return ['message'=>'请先点击发货!'];

                    $order_new_time = time()-$order_result['update_time'];

                    if($order_new_time <= $order_time)return ['message'=>'超过订单处理期间才能操作!'];
                }else{
                    if($order_goods_result['status'] != '3')return ['message'=>'请先确认收货!'];
                }

            }

            if(!in_array($order_goods_status, ['2','5'])){
                $order_result = db('order')->where(['id'=>$order_id])->find();
                if($order_result['user_id'] != $this->user['id'])return ['message'=>'你没有权利更改别人的订单!'];
            }

            $order_handle = db('order_goods')->where(['order_id'=>$order_id,'goods_id'=>$goods_id])->update(['status'=>$order_goods_status]);

            if($order_handle){
                //当所有订单都完成时则把订单表更新以完成
                $order_goods = db('order_goods')->where(['order_id'=>$order_id])->count();
                $order_goods_status = db('order_goods')->where(['order_id'=>$order_id,'status'=>'5'])->count();
                if($order_goods == $order_goods_status){
                    db('order')->where(['id'=>$order_id])->update(['status'=>'3']);
                }
            }

            if($order_handle && $order_goods_result['status'] != '4' && $order_goods_status == '5' && empty($order_return)){
                //订单产品完成
                $data = [];
                $data['user_id'] = $order_goods_result['buyer_user_id'];
                $data['price'] = $order_goods_result['goods_price'];
                $data['order_goods_id'] = $order_goods_result['id'];
                $data['create_time'] = time();

                //钱入账到个人账户
                db('wallet')->insert($data);
            }
        }
    }

    public function orderReturn(){
        $order_id = $this->requestData['order_id'];
        $goods_id = $this->requestData['goods_id'];
        $remarks = $this->requestData['remarks'];

        $order_result = db('order')->where(['id'=>$order_id])->find();

        if(empty($order_result))return ['message'=>'订单参数有误!'];
        
        if($order_result['user_id'] != $this->user['id'])return ['message'=>'你没有权限更改别人的订单!'];

        $order_goods_result = db('order_goods')->where(['order_id'=>$order_id,'goods_id'=>$goods_id])->find();

        if(empty($order_goods_result))return ['message'=>'该订单没有这商品'];

        $order_return_result = db('order_return')->where(['order_id'=>$order_id,'goods_id'=>$goods_id,'status'=>'0','money_arrives_status'=>'0'])->find();
        if(!empty($order_return_result))return ['message'=>'你已申请过退货，请等待审核!'];

        $pay_log = db('pay_log')->where(['record_id'=>$order_id])->value('status');

        if($pay_log != '1')return ['message'=>'该订单未付款!'];

        $order_goods_status = db('order_goods')->where(['goods_id'=>$goods_id,'order_id'=>$order_id])->update(['status'=>'4']);

        $order_goods_result['remarks'] = $remarks;
        $order_goods_result['refund_sn'] = 'R'.$this->generateOrderNo();
        $order_goods_result['creatime'] = time();
        unset($order_goods_result['status']);
        unset($order_goods_result['id']);
        $order_return = db('order_return')->insertGetId($order_goods_result);

        if(!$order_return)return ['message'=>'退货申请失败!'];
        $result['return_id'] = $order_return;
        return $result;
    }

    public function orderReturnImg(){
        $return_id = $this->requestData['return_id'];

        $order_return = db('order_return')->where(['id'=>$return_id])->find();

        if(empty($order_return))return ['message'=>'return_id参数有误!'];

        $order_return['certificate_image'] = json_decode($order_return['certificate_image'],1);

        $file = request()->file('image');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public/static/uploads');
            if ($info) {
                $order_return['certificate_image'][] = '/'.$info->getSaveName();
            }

            $order_return['certificate_image'] = json_encode($order_return['certificate_image']);
            $data['certificate_image'] = $order_return['certificate_image'];
            db('order_return')->where(['id'=>$return_id])->update($data);
        }
    }

    public function orderReturnUpdate(){
        $order_id = $this->requestData['order_id'];
        $goods_id = $this->requestData['goods_id'];
        $status = $this->requestData['status'];

        if(!in_array($status,['1','-1']))return ['message'=>'status参数有误!'];

        $order_goods_result = db('order_goods')->where(['order_id'=>$order_id,'goods_id'=>$goods_id])->find();

        if($order_goods_result['buyer_user_id'] != $this->user['id'])return ['message'=>'你没有权限更改别人的订单!'];

        if(empty($order_goods_result))return ['message'=>'该订单没有这商品!'];

        db('order_return')->where(['order_id'=>$order_id,'goods_id'=>$goods_id])->update(['status'=>$status]);
    }

    public function orderReturnList(){
        $page = isset($this->requestData['page'])?$this->requestData['page']:'1';
        $order_return = db('order_return')->where(['buyer_user_id'=>$this->user['id']])->page($page,10)->order('id desc')->select();
        $circle_name = array_column($order_return, 'circle_name');
        $circle_name = array_unique($circle_name);
        $circle_name = array_values($circle_name);
        $data = [];
        foreach ($circle_name as $k => $v) {
            foreach ($order_return as $kk => $vv) {
                if($vv['circle_name'] == $v){
                    $data[$k]['circle_name'] = $v;
                    $vv['goods_image'] = IMG_PATH.$vv['goods_image'];
                    if(!empty($vv['certificate_image'])){
                        $vv['certificate_image'] = @json_decode($vv['certificate_image'],1);
                        foreach ($vv['certificate_image'] as $key => &$value) {
                            $value = IMG_PATH.$value;
                        }
                    }else{
                        $vv['certificate_image'] = [];
                    }
                    $vv['goods_price'] = $vv['goods_price']/100;
                    $data[$k]['return_goods'][] = $vv;
                }
            }
        }

        return $data;
    }

    public function orderReturnInfo(){
        $return_id = $this->requestData['return_id'];
        $order_return = db('order_return')->where(['id'=>$return_id])->find();
        $order_return['certificate_image'] = json_decode($order_return['certificate_image'],1);

        if(!empty($order_return['certificate_image'])){
            foreach ($order_return['certificate_image'] as $key => &$value) {
                $value = IMG_PATH.$value;
            }
        }else{
            $order_return['certificate_image'] = [];
        }
        $order_return['goods_price'] = $order_return['goods_price']/100;
        $order_return['goods_image'] = IMG_PATH.$order_return['goods_image'];

        return $order_return;
    }


    /**
    * 订单号
    */
    public function generateOrderNo()
    {
        $yearCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn =
            $yearCode[intval(date('Y')) - 2017] . strtoupper(dechex(date('m'))) . date(
                'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
                '%02d', rand(0, 999));
        return $orderSn;
    }

    /**
    *  判断是否还有库存
    */
    public function stock_bool($goods_result){
        if(!is_array($goods_result))return ['message'=>'false'];
        if(empty($goods_result))return ['message'=>'false'];

        $bool = true;
        $no_goods = [];
        foreach ($goods_result as $k => $v) {
            if(isset($v['goods_result'])){
                foreach ($v['goods_result'] as $kk => $vv) {
                    if($vv['goods_stock'] < $vv['goods_total']){
                        $bool = false;
                        $no_goods = $vv;
                        break;
                    }
                }
            }
        }

        if(!$bool){
            return ['message'=>'false','no_goods'=>$no_goods];
        }else{
            return ['message'=>'true'];
        }
    }

    /**
    *   商品评论
    */
    public function orderReview(){
        $order_id = $this->requestData['order_id'];                //收货地址
        $goods_id = $this->requestData['goods_id'];                //购买商品

        $order_goods = db('order_goods')->where(['order_id'=>$order_id,'goods_id'=>$goods_id])->field('status,buyer_user_id,cid')->find();
        if(empty($order_goods))return ['message'=>'order_id或goods_id参数非法!'];
        if($order_goods['status'] != '5')return ['message'=>'请完成订单在评论!'];

        $data['user_id'] = $this->user['id'];

        $data['content'] = $this->requestData['content'];
        $data['order_id'] = $order_id;
        $data['goods_id'] = $goods_id;
        $data['cid'] = $order_goods['cid'];

        $member = db('member')->where(['id'=>$this->user['id']])->field('headimage,nickname')->find();
        $data['headimage'] = $member['headimage'];
        $data['nickname'] = $member['nickname'];
        $data['creatime'] = time();

        $order_review = db('order_review')->where(['user_id'=>$this->user['id'],'order_id'=>$order_id,'goods_id'=>$goods_id])->find();

        if(!empty($order_review))return ['message'=>'已评论过了!'];

        $order_review = db('order_review')->insert($data);

        if(!$order_review)return ['message'=>'评论失败!'];
    }
}